<template>
  <div class="dashboard-editor-container">
    <el-form :inline="true" :model="classRequest">
      <el-form-item label="班级名称">
        <el-input v-model="classRequest.className" placeholder="请输入班级名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="classRequest.status" placeholder="班级状态" clearable>
          <el-option label="正常" value="1"></el-option>
          <el-option label="停用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级专业">
        <el-select v-model="classRequest.profession" placeholder="请选择班级专业" clearable>
          <el-option value="">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-refresh">重置</el-button>
      </el-form-item>
      <br>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="goAdd">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="warning" icon="el-icon-circle-check">启用</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="warning" icon="el-icon-circle-close">停用</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="danger" icon="el-icon-s-grid">设置毕业</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" icon="el-icon-s-claim">设置任课老师</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" icon="el-icon-menu">导入班级信息</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="序号"
      >
      </el-table-column>
      <el-table-column
        prop="className"
        label="班级名称"
      >
      </el-table-column>
      <el-table-column
        prop="teacherName"
        label="班主任"
      >
      </el-table-column>
      <el-table-column
        prop="stuNum"
        label="班级人数"
      >
      </el-table-column>
      <el-table-column
        prop="classGradeStr"
        label="班级年级"
      >
      </el-table-column>
      <el-table-column
        prop="tdataClassProfessionStr"
        label="班级专业"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        label="是否毕业"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.isGraduation === 1 ? '已毕业' : '未毕业' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="'1'"
            :inactive-value="'0'"
            @change="changeStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作" width="200px"
      >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="onUpdate(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="godel(scope.row)"></el-button>
          <el-button icon="el-icon-user-solid" size="mini" circle  @click="checkStu(scope.row)"></el-button>
          <el-button icon="el-icon-s-management" size="mini" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="sizes, prev, pager, next"
      :total="totalCount"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="classRequest.pageSize"
      :current-page="classRequest.pageNo"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <el-dialog
      title="添加|修改 班级信息"
      :visible.sync="dialogVisible"
      width="45%"
    >
      <el-form ref="ruleForm" :inline="true" :rules="rules" :model="classModel">
        <el-form-item label="班级名称" prop="className">
          <el-input v-model="classModel.className" placeholder="请输入班级名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="班级专业">
          <el-select clearable v-model="classModel.classType" placeholder="请选择">
            <el-option v-for="item in zhuanye"
                       :key="item.id"
                       :label="item.dictLabel"
                       :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级年级" prop="classGrade">
          <el-select clearable v-model="classModel.classGrade" placeholder="请选择">
            <el-option v-for="item in nainji"
                       :key="item.id"
                       :label="item.dictLabel"
                       :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班主任" prop="classTeacherId">
          <el-select clearable v-model="classModel.classTeacherId" placeholder="请选择">
            <el-option v-for="item in banzhuren"
                       :label="item.username"
                       :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="助理班主任">
          <el-select clearable v-model="classModel.assistantTeacharId" placeholder="请选择">
            <el-option v-for="item in zhulibzr"
                       :key="item.id"
                       :label="item.username"
                       :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学期">
          <el-select clearable v-model="classModel.semester" placeholder="请选择">
            <el-option v-for="item in xueqi"
                       :key="item.id"
                       :label="item.dictLabel"
                       :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="classModel.status" label="1">启用</el-radio>
          <el-radio v-model="classModel.status" label="0">停用</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="resetForm('ruleForm')">取 消</el-button>
    <el-button type="primary" @click="subAdd('ruleForm')">确 定</el-button>
  </span>
    </el-dialog>
<!-- 查看学生-->
    <el-dialog
      title="班级学生信息"
      :visible.sync="dialogVisibleClassStu"
      width="50%"
       >

    </el-dialog>
  </div>
</template>

<script>

import request from '@/utils/request'
import * as url from 'url'

export default {
  name: 'Index',

  data() {
    return {
      stuList:[],
      dialogVisibleClassStu:false,
      rules: {
        className: [
          { required: true, message: '请输入班级名称', trigger: 'blur' }
        ],
        classGrade: [
          { required: true, message: '请输入班级名称', trigger: 'blur' }
        ],
        classTeacherId: [
          { required: true, message: '请输入班级名称', trigger: 'blur' }
        ]
      },
      zhuanye: [],
      nainji: [],
      banzhuren: [],
      zhulibzr: [],
      xueqi: [],
      classModel: {
        id: '',
        status: '1',
        className: '',
        classType: '', //专业
        classGrade: '',
        classTeacherId: '',
        assistantTeacharId: '',
        semester: ''
      },
      dialogVisible: false,
      tableData: [],
      totalCount: 0,
      totalPageCount: 0,
      classRequest: {
        className: '',
        status: '',
        profession: '', //专业
        pageNo: 1,
        pageSize: 1
      },
      flag: '',
      roleFlag: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkStu(row){
      request({
        url: '/data/class/checkStu/'+row.id,
        method: 'get',
      }).then(res => {
        console.log(res)
        this.stuList=res.data;
        console.log(this.stuList)
      })
      this.dialogVisibleClassStu=true;
    },
    changeStatus(row){
      request({
        url: '/data/class/changeStatus/'+row.id,
        method: 'get',
      }).then(res => {
        // console.log(res)
        if (res.code === 200) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    godel(row){
      if (confirm('确认删除？')) {
        request({
          url: '/data/class/del/'+row.id,
          method: 'get',
        }).then(res => {
          // console.log(res)
          if (res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }
    },
    onUpdate(row) {
      // alert(row.id)
      this.goAdd()
      // console.log(this.classModel)
      this.classModel.id = row.id;
      this.classModel.status = row.status;
      this.classModel.className = row.className;
      this.classModel.classType = row.classType;
      this.classModel.classGrade = row.classGrade;
      this.classModel.classTeacherId = row.classTeacherId;
      this.classModel.assistantTeacharId = row.assistantTeacharId;
      this.classModel.semester = row.semester;
    },
    subAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          request({
            url: '/data/class/addClass',
            method: 'post',
            data: this.classModel
          }).then(res => {
            // console.log(res)
            if (res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.resetForm('ruleForm')
              this.getList()
            }
          })
        } else {
          alert('请填写完整数据')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.classModel.id = '';
      this.classModel.status = '1';
      this.classModel.className = '';
      this.classModel.classType = '';
      this.classModel.classGrade = '';
      this.classModel.classTeacherId = '';
      this.classModel.assistantTeacharId = '';
      this.classModel.semester = '';
      this.classModel.classType = ''
      this.dialogVisible = false
    },
    goAdd() {
      this.dialogVisible = true
      request({
        url: '/data/class/getData',
        method: 'get'
      }).then(res => {
        // console.log(res.data)
        this.zhuanye = res.data.zhuanyeList
        this.nainji = res.data.gradeList
        this.banzhuren = res.data.aTeachers
        // console.log(res.data.aTeachers)
        this.zhulibzr = res.data.bTeachers
          // console.log(this.zhulibzr)
        this.xueqi = res.data.semesterList
        // console.log(this.xueqi)
      })
    },
    handleSizeChange(val) {
      this.classRequest.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.classRequest.pageNo = val
      this.getList()
    },
    getList() {
      request({
        url: '/data/class/list',
        method: 'get',
        params: this.classRequest
      }).then(response => {
          console.log(response.data)
        this.tableData = response.data.data
        this.totalCount = response.data.totalCount
        this.totalPageCount = response.data.totalPageCount
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: white;
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}
</style>
